package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.geom.Point;
	
	[SWF(width=265, height=275)]
	public class PerlinLine extends Sprite
	{
		public var pixels:Vector.<uint>;
		public var bmd:BitmapData;
		public var bmp:Bitmap;
		public var sp:Sprite;
		public var canvData:BitmapData;
		public var canv:Bitmap;
		public var offset:Array = [new Point(0,0), new Point(0,0)];
		
		public var blue:int = 0xFF;
		public var green:int = 0x00;
		public var red:int = 0x00;
		
		public var p:Point = new Point(0,0);
		public var blur:BlurFilter = new BlurFilter(3,3,3);
		
		public function PerlinLine()
		{
			sp = new Sprite();
			canvData = new BitmapData(250,255,false,0);
			canv = new Bitmap(canvData);
			canv.x = 10;
			canv.y = 20;
			this.addChild(canv);
			
			bmd = new BitmapData(250,1,false,0);
			bmp = new Bitmap(bmd);
			bmp.y = 10;
			bmp.x = 10;
			bmp.height = 10;
			this.addChild(bmp);
			
			this.addEventListener(Event.ENTER_FRAME, step);
		}
		
		public function step(e:Event=null):void
		{
			bmd.perlinNoise(60,60, 2, 1000,false, true, 7, true, offset);
			pixels = bmd.getVector(bmd.rect);
			
			var color:uint = (red<<16) + (green<<8) + blue;
			
			var g:Graphics = sp.graphics;
			g.clear();
			g.lineStyle(1,color);
			g.moveTo(0,-(pixels[0]>>16));
			for(var i:int=1; i<pixels.length; i++)
			{
				g.lineTo(i, -(pixels[i]>>16));
			}
			canvData.draw(sp);
			
			if(blue == 0xFF && green < 0xFF && red == 0)
			{
				green++;
			}
			else if(blue > 0 && green == 0xFF && red == 0)
			{
				blue--;
			}
			else if(blue == 0 && green == 0xFF && red < 0xFF)
			{
				red++;
			}
			else if(blue == 0 && green > 0 && red == 0xFF)
			{
				green--;
			}
			else if(blue < 0xFF && green == 0 && red == 0xFF)
			{
				blue++;
			}
			else if(blue == 0xFF && green == 0 && red > 0)
			{
				red--;
			}
			
			offset[0].x += .2;
			offset[0].y += .2;
			offset[1].x += -.2;
			offset[1].y += -.2;
			
			canvData.scroll(0,-2);
			canvData.applyFilter(canvData, canvData.rect, p, blur);
		}
	}
}